package com.gaoyi.basemodule.baseUtil;

import android.view.Gravity;
import android.widget.Toast;

public class ToastUtil {
    private static Toast toast = null;

    public static void showToast(String message) {
        try {
            OKHttpUtil.getInstance().post(() -> {
                if (toast != null) {
                    toast.cancel();
                }
                toast = Toast.makeText(AppUtil.getContext(), message, Toast.LENGTH_SHORT);
                toast.show();
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void showToast(int message) {
        try {
            OKHttpUtil.getInstance().post(() -> {
                if (toast != null) {
                    toast.cancel();
                }
                toast = Toast.makeText(AppUtil.getContext(), message, Toast.LENGTH_SHORT);
                toast.show();
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void showToast(int message, int gravity, int xOffset, int yOffset) {
        try {
            OKHttpUtil.getInstance().post(() -> {
                if (toast != null) {
                    toast.cancel();
                }
                toast = Toast.makeText(AppUtil.getContext(), message, Toast.LENGTH_SHORT);
                toast.setGravity(gravity, xOffset, yOffset);
                toast.show();
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void showToast(String message, int gravity, int xOffset, int yOffset) {
        try {
            OKHttpUtil.getInstance().post(() -> {
                if (toast != null) {
                    toast.cancel();
                }
                toast = Toast.makeText(AppUtil.getContext(), message, Toast.LENGTH_SHORT);
                toast.setGravity(gravity, xOffset, yOffset);
                toast.show();
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void showToastInTop(int message){
        try {
            OKHttpUtil.getInstance().post(() -> {
                if (toast != null) {
                    toast.cancel();
                }
                toast = Toast.makeText(AppUtil.getContext(), message, Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.TOP,0,40);
                toast.show();
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void showToastInTop(String message){
        try {
            OKHttpUtil.getInstance().post(() -> {
                if (toast != null) {
                    toast.cancel();
                }
                toast = Toast.makeText(AppUtil.getContext(), message, Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.TOP,0,40);
                toast.show();
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
